const app = getApp()
Page({
  data: {
    id: 0,
    imageList: [],
    totalMin: 0,
    totalMax: 0,
    image: {},
    imageIndex: 0,
    deleteDialogShow: false,
  },
  // 删除图片
  deleteImage() {
    const { imageList, imageIndex, image } = this.data
    let arr = imageList.filter((item, index) => index !== imageIndex)
    this.setImageList(arr)
    this.handleDeleteDialogShow()
    if (!arr.length) {
      my.navigateBack({
        delta: 1
      })
    }
    if (imageIndex > 0) {
      this.setData({
        imageIndex: imageIndex - 1
      })
    }
  },
  // 更改图片列表
  setImageList(arr) {
    this.setData({
      imageList: arr
    })
    app.globalData.imageList = arr
  },
  // 切换图片
  changeImage(e) {
    let imageIndex = e.detail.current
    let image = this.data.imageList.find((item, index) => index === imageIndex)
    this.setData({
      image,
      imageIndex
    })
  },
  handleDeleteDialogShow() {
    this.setData({
      deleteDialogShow: !this.data.deleteDialogShow
    })
  },
  // 绑定数据
  bindData(query) {
    let id = Number(query.id)
    let imageList = app.globalData.imageList
    let image = imageList.find(v => v.id === id)
    let imageIndex = imageList.findIndex(v => v.id === id)
    this.setData({
      imageList,
      totalMin: app.globalData.totalMin,
      totalMax: app.globalData.totalMax,
      id,
      image,
      imageIndex
    })
  },
  onLoad(query) {
    this.bindData(query)
  },
})
